package com.yuan.plugin.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class RSungDBHelper extends SQLiteOpenHelper{

	public static final String DBNAME = "rsungdb";
	public static final int DBVERSION = 1;
	
	public static final String TEMP_SQL_CREATE_TABLE_SUBSCRIBE = "alter table " + "A" + " rename to temp_A";
	public static final String sql_class_table="create table if not exists notebook(_id integer primary key autoincrement,rs_index text,rs_name text,rs_job text,rs_img number,rs_type number,rs_uri text)";
	public static final String sql_sc_history="create table if not exists s_history(_id integer primary key autoincrement,s_content text,s_time text,flag text)";
	public static final String sql_od_sc_history="create table if not exists ods_history(_id integer primary key autoincrement,s_content text,s_time text,flag text)";
	
	
	public static RSungDBHelper instance;
	private SQLiteDatabase mDB = null;
	
	public static List<SQLiteDatabase> dbs;
	
	/**
	 * helper 单例
	 * @param context
	 * @return
	 */
	public static RSungDBHelper getInstance(Context context){
		if(instance == null){
			instance = new RSungDBHelper(context);
			dbs = new ArrayList<SQLiteDatabase>();
		}
		
		return instance;
	}
	/**
	 * 打开数据库
	 */
	public synchronized SQLiteDatabase openDB(){
		if(mDB == null){
            mDB = instance.getWritableDatabase();
        }
		for (SQLiteDatabase db:dbs) {
			if (mDB != db) {
				dbs.add(mDB);
			}
		}
		return mDB;
	}
	/**
	 * 关闭数据库
	 */
	public static void closeDB(){
		for (SQLiteDatabase db:dbs) {
			db.close();
		}
	}
	/**
	 * helper init
	 * @param context
	 */
	public RSungDBHelper(Context context) {
		super(context, DBNAME, null, DBVERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		db.execSQL(sql_class_table);
		db.execSQL(sql_sc_history);
		db.execSQL(sql_od_sc_history);
		//表存在则不创建
		/**
         * 这个方法
         * 1、在第一次打开数据库的时候才会走
         * 2、在清除数据之后再次运行-->打开数据库，这个方法会走
         * 3、没有清除数据，不会走这个方法
         * 4、数据库升级的时候这个方法不会走
         */
//		db.execSQL( "CREATE TABLE IF NOT EXISTS "+
//
//                TB_NAME+ "("+
//
//                UserInfo. ID+ " integer primary key,"+
//
//                UserInfo. USERID+ " varchar,"+
//
//                UserInfo. TOKEN+ " varchar,"+
//
//                UserInfo. TOKENSECRET+ " varchar,"+
//
//                UserInfo. USERNAME+ " varchar,"+
//
//                UserInfo. USERICON+ " blob"+
//
//                ")"
//
//                );
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		/**
         * 1、第一次创建数据库的时候，这个方法不会走
         * 2、清除数据后再次运行(相当于第一次创建)这个方法不会走
         * 3、数据库已经存在，而且版本升高的时候，这个方法才会调用
         */
		
		onCreate(db);
	}
	/**
	 * 数据库表是否存在
	 * @param tabName
	 * @param db
	 * @return
	 */
	public boolean tableIsExist(String tabName, SQLiteDatabase db){
        boolean result = false;
        if(tabName == null){
        	return false;
        }
        Cursor cursor = null;
        try {
        	String sql = "select count(*) as c from sqlite_master where type ='table' and name ='"+tabName.trim()+"' ";
            cursor = db.rawQuery(sql, null);
            if(cursor.moveToNext()){
            	int count = cursor.getInt(0);
            	if(count>0){
            		result = true;
            		}
                }
        } catch (Exception e) {
        	
        }                
        return result;
}
}
